home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libsock / todo.c < prev    next >
C/C++ Source or Header  |  1997-08-03  |  4KB  |  174 lines

  1. /* todo.c:  Translate Pilot ToDo application data formats
  2.  *
  3.  * Copyright (c) 1996, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pi-source.h"
  13. #include "pi-dlp.h"
  14. #include "pi-todo.h"
  15.  
  16. void free_ToDo(struct ToDo * a) {
  17.   if(a->description)
  18.     free(a->description);
  19.   if(a->note)
  20.     free(a->note);
  21. }
  22.  
  23. int unpack_ToDo(struct ToDo * a, unsigned char * buffer, int len) {
  24.   unsigned long d;
  25.   unsigned char * start = buffer;
  26.  
  27.   /* Note: There are possible timezone conversion problems related to the
  28.            use of the due member of a struct ToDo. As it is kept in local
  29.            (wall) time in struct tm's, the timezone of the Pilot is
  30.            irrelevant, _assuming_ that any UNIX program keeping time in
  31.            time_t's converts them to the correct local time. If the Pilot is
  32.            in a different timezone than the UNIX box, it may not be simple
  33.            to deduce that correct (desired) timezone.
  34.                                                                                
  35.            The easiest solution is to keep apointments in struct tm's, and
  36.            out of time_t's. Of course, this might not actually be a help if
  37.            you are constantly darting across timezones and trying to keep
  38.            appointments.
  39.                                                                     -- KJA
  40.            */
  41.  
  42.                                                                                                                                                                                                           
  43.   if (len< 3)
  44.     return 0;
  45.   d = (unsigned short int)get_short(buffer);
  46.   if (d != 0xffff) {
  47.     a->due.tm_year = (d >> 9) + 4;
  48.     a->due.tm_mon = ((d >> 5) & 15) - 1;
  49.     a->due.tm_mday = d & 31;
  50.     a->due.tm_hour = 0;
  51.     a->due.tm_min = 0;
  52.     a->due.tm_sec = 0;
  53.     a->due.tm_isdst = -1;
  54.     mktime(&a->due);
  55.     a->indefinite = 0;
  56.   } else {
  57.     a->indefinite = 1; /* a->due is invalid */
  58.   }
  59.  
  60.   a->priority = get_byte(buffer+2);
  61.   if(a->priority & 0x80) {
  62.     a->complete = 1;
  63.     a->priority &= 0x7f;
  64.   } else {
  65.     a->complete = 0;
  66.   }
  67.   
  68.   buffer +=3;
  69.   len -= 3;
  70.   
  71.   if (len<1)
  72.     return 0;
  73.   a->description = strdup((char*)buffer);
  74.   
  75.   buffer += strlen(a->description)+1;
  76.   len -= strlen(a->description)+1;
  77.   
  78.   if (len<1) {
  79.     free(a->description);
  80.     a->description=0;
  81.     return 0;
  82.   }
  83.   a->note = strdup((char*)buffer);
  84.   
  85.   buffer += strlen(a->note)+1;
  86.   len -= strlen(a->note)+1;  
  87.   
  88.   return (buffer-start); /* FIXME: return real length*/
  89. }
  90.  
  91. int pack_ToDo(struct ToDo *a, unsigned char * buf, int len) {
  92.   int pos;
  93.   int destlen = 3;
  94.   if (a->description)
  95.     destlen+=strlen(a->description);
  96.   destlen++;
  97.   if (a->note)
  98.     destlen+=strlen(a->note);
  99.   destlen++;
  100.   
  101.   if (!buf)
  102.     return destlen;
  103.   if (len<destlen)
  104.     return 0;
  105.  
  106.   if (a->indefinite) {
  107.     buf[0] = 0xff;
  108.     buf[1] = 0xff;
  109.   } else {
  110.     set_short(buf, ((a->due.tm_year - 4) << 9) |
  111.                    ((a->due.tm_mon  + 1) << 5) |
  112.                    a->due.tm_mday);
  113.   }
  114.   buf[2] = a->priority;
  115.   if(a->complete) {
  116.     buf[2] |= 0x80;
  117.   }
  118.   
  119.   pos = 3;
  120.   if(a->description) {
  121.     strcpy((char*)buf+pos, a->description);
  122.     pos += strlen(a->description)+1;
  123.   } else {
  124.     buf[pos++] = 0;
  125.   }
  126.   
  127.   if(a->note) {
  128.     strcpy((char*)buf+pos, a->note);
  129.     pos += strlen(a->note)+1;
  130.   } else {
  131.     buf[pos++] = 0;
  132.   }
  133.   
  134.   return pos;
  135. }
  136.  
  137.                   
  138. int unpack_ToDoAppInfo(struct ToDoAppInfo * ai, unsigned char * record, int len) {
  139.   int i;
  140.   unsigned char * start = record;
  141.   i = unpack_CategoryAppInfo(&ai->category, record, len);
  142.   if (!i)
  143.     return 0;
  144.   record += i;
  145.   len -= i;
  146.   if (len<4)
  147.     return 0;
  148.   ai->dirty = get_short(record);
  149.   record += 2;
  150.   ai->sortByPriority = get_byte(record);
  151.   record += 2;
  152.   return (record-start);
  153. }
  154.  
  155. int pack_ToDoAppInfo(struct ToDoAppInfo * ai, unsigned char * record, int len) {
  156.   int i;
  157.   unsigned char * start = record;
  158.   i = pack_CategoryAppInfo(&ai->category, record, len);
  159.   if (!record)
  160.     return i+4;
  161.   if (!i)
  162.     return 0;
  163.   record += i;
  164.   len -= i;
  165.   if (len < 4)
  166.     return 0;
  167.   set_short(record, ai->dirty);
  168.   set_byte(record+2, ai->sortByPriority);
  169.   set_byte(record+3, 0); /* gapfil */
  170.   record += 4;
  171.   
  172.   return (record-start);
  173. }
  174.